/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unc.pds.model;

import java.rmi.RemoteException;
import java.util.Collection;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EJBHome;
import javax.ejb.EJBObject;
import javax.ejb.Handle;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import unc.pds.data.Consts;
import unc.pds.data.RootDataBeanRemote;
import unc.pds.data.RootDataBeanRemoteHome;
import unc.pds.util.Arch;
import unc.pds.util.ArchImpl;

/**
 *
 * @author AIN
 */
public class Role implements SessionBean {

    private static String default_object_type = Consts.OBJECT_TYPE__ROLE;
    private int level = 0;
    private SessionContext context;
    private Long sessionKey;
    unc.pds.data.RootDataBeanRemote dataBean;

    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">;
    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    // TODO Add business methods or web service operations
    /**
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    public void setSessionContext(SessionContext aContext) {
        context = aContext;
    }

    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
    }

    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
    }

    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() throws RemoteException {
    }

    // </editor-fold>;
    /**
     * See section 7.10.3 of the EJB 2.0 specification
     * See section 7.11.3 of the EJB 2.1 specification
     */
    /*public void ejbCreate() {
    }*/

    public void ejbCreate(unc.pds.data.RootDataBeanRemote rdb,Long nsessionKey) throws RemoteException {
        dataBean = rdb;
        sessionKey = nsessionKey;
        dataBean.setSessionKey(sessionKey);
    }

    public void ejbCreate(Long id, Long nsessionKey) throws NamingException, RemoteException {
        try {
            RootDataBeanRemoteHome rdbrh = (RootDataBeanRemoteHome) (new InitialContext()).lookup("ejb/RootDataBean");
            dataBean = rdbrh.findByPrimaryKey(id);
        } catch (Exception e) {
            throw new EJBException(e.getLocalizedMessage());
        }
        sessionKey = nsessionKey;
        dataBean.setSessionKey(sessionKey);
    }

    public void ejbCreate(Arch doc, Long nsessionKey) throws CreateException, RemoteException {
        sessionKey = nsessionKey;        
        try {
            if (doc.getChild(Consts.ARCH_F_TYPE) == null) {
                doc.addChild(new ArchImpl(Consts.ARCH_F_TYPE, Consts.OBJECT_TYPE__ROLE));
            }
            RootDataBeanRemoteHome rdbrh = (RootDataBeanRemoteHome) (new InitialContext()).lookup("ejb/RootDataBean");
            dataBean = rdbrh.create(doc,sessionKey);
            dataBean.setSessionKey(sessionKey);
        } catch (Exception nex) {
            if (Consts.DEBUG_MODE) System.out.println("ERROR: Role: CREATEbyDOC: " + nex.getLocalizedMessage());
            throw new CreateException("Role: CREATEbyDOC: " + nex.getLocalizedMessage());
        }

        
    }

    public Long getKey() throws RemoteException {
        return (Long) dataBean.getPrimaryKey();
    }

    public Model getParent() throws RemoteException {
        return ModelFactory.getByKey(dataBean.getParentID(),sessionKey);
    }

    public void dropMe() throws RemoteException {
        try {
            dataBean.dropMe();
        } catch (Exception e) {
            throw new RemoteException(e.getLocalizedMessage());
        }
    }

    public void setLevel(int num) throws RemoteException {
        level = num;
    }

    public int getLevel() throws RemoteException {
        return level;
    }

    public boolean isSuprOrEqual(RoleRemote role) throws RemoteException {
        if (role.getKey().compareTo(dataBean.getID()) == 0) {
            return true;
        }
        try {
            RootDataBeanRemoteHome rdbrh = (RootDataBeanRemoteHome) (new InitialContext()).lookup("ejb/RootDataBean");
            Collection beans = rdbrh.findByParentKey(dataBean.getID(), Consts.OBJECT_TYPE__ROLE);
            for (int i=0;i<beans.size();i++){
                ((RootDataBeanRemote)beans.toArray()[i]).setSessionKey(sessionKey);
                RoleRemoteHome rrh = (RoleRemoteHome) (new InitialContext()).lookup("ejb/Role");
                RoleRemote temprole = rrh.create((RootDataBeanRemote)beans.toArray()[i],sessionKey);
                if (temprole.isSuprOrEqual(role)) return true;
            }
        } catch (Exception e) {
            throw new RemoteException("ERROR: Role.isSubOrEqual: " + e.getLocalizedMessage());
        }
        return false;
    }

    public String getName() throws RemoteException{
        return dataBean.getParameter(Consts.ATTR__ROLE_NAME);
    }
}
